#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#define FD_MAX 1024

int my_dup2(int src,int des){
   
   if(src == des){
    
       return des;
   } 
    ssize_t fds[FD_MAX];   
    int i,index= 0;
    for(i = 0; i < FD_MAX; i++){
        
        fds[i] = 0;;
    }
    
    close(des);
    for(;;){
        
       int n_fd =  dup(src);   
       if(n_fd != des ){
        
           fds[index++] = n_fd;
       }else{
           break;
       }
    }

    for(i = 0;i < index;i++){
        
        close(fds[i]);
    }
    return des;
}

int
main(int argc, char *argv[]){
    
    int fd = 5;
    my_dup2(STDOUT_FILENO,fd);
    
    write(fd,"hs\n",3);
       
    return 0;
}
